window.onload = function () {
    // 获取个人信息
    var items = document.getElementsByClassName("item");
    //获取姓名输入框
    var name = document.getElementById("name");
    //获取身份证输入框
    var idNumber= document.getElementById("id_number");
    //获取下一步按钮
    var btnNext= document.getElementById("btnNext");
    //获取数字键盘的容器
    var number= document.getElementById("number");
    //获取键盘上的所有数字
    var spans = number.getElementsByTagName("span");
    //获取数字键盘上的清除按钮
    var clear= document.getElementById("clear");
    //获取身份证号后的清空按钮
    var empty= document.getElementById("empty");
    //获取正确提示框
    var success = document.getElementById("success");
    //获取错误提示框
    var fail = document.getElementById("fail");
    //获取错误提示框的确定按钮
    var  btnOk= document.getElementById("btn_ok");
    //获取已阅读并同意的单选框
    var btnAgree = document.getElementById("btn-agree");
    var agree = document.getElementById("agree");
    console.log(btnOk);

    for(var j = 0;j<items.length;j++) {
        items[j].suoYin = j;
        items[j].onfocus = function () {
            this.value = "";
        }
    }

    //点击哪个数字，就把哪个数字填入身份证输入框
    var index = 0;
    for(var i = 0; i<spans.length-1;i++){
        spans[i].indexes = i;
        spans[i].onclick = function() {
            index = this.indexes;
            idNumber.value += spans[index].innerHTML;
            if (idNumber.value.length == 6 || idNumber.value.length == 15){
                idNumber.value += " ";
            }
        }
    }
    //点击数字键盘上面的清除按钮，清除一位
    clear.onclick = function(){
        idNumber.value = idNumber.value.slice(0,idNumber.value.length-1);
    }
    //点击身份证输入框后面的清除按钮，清除输入框的内容
    empty.onclick = function () {
        idNumber.value = "";
    }
    //勾选我已阅读并同意《服务协议》
    var countTime = 0;
    btnAgree.onclick = function(){
        countTime++;
        if (countTime % 2 != 0) {
            agree.style.display = "inline-block";
        }else {
            agree.style.display = "none";
        }
    }
    //点击下一步对姓名输入框和身份证输入框进行验证
    function timeLapse() {
        success.style.display = "none";
    }
    btnNext.onclick = function () {
        var regName =/^[\u4e00-\u9fa5]{2,4}$/;
        var reg = /^[1-9]{1}[0-9]{5}\s[1-9]{1}[0-9]{7}\s[0-9]{4}$/;
        if(!regName.test(name.value) || !reg.test(idNumber.value)){
            fail.style.display = "block";
        }else{
            success.style.display = "block";
            setTimeout(timeLapse,2000);
        }
    }
    // 点击错误提示框上的确定按钮，关闭错误提示框
    btnOk.onclick = function () {
        fail.style.display = "none";
    }
    // 点击身份证输入框弹出数字键盘
    var time = 0;
    idNumber.onclick = function () {
        time++;
        if (time % 2 !=0){
            number.style.display = "block";
        }else{
            number.style.display = "none";
        }
    }
}
